const express = require("express")
const app = express();
const path = require("path");

//处理body请求体的中间件
app.use(express.urlencoded())
app.use(express.json())

// //处理跨域请求的中间件(方便OPTIONS请求进来获取权限)
// app.use((req, res, next) => {
//   //所有的请求都允许跨域
//   // res.set("Access-Control-Allow-Origin", "*")
//   //设置允许跨域的ip地址
//   // res.set("Access-Control-Allow-Origin", "http://127.0.0.1:57558")

//   //设置允许允许跨域的地址集合
//   const allowAdd = ["http://127.0.0.1:57555", "http://127.0.0.1:57556", "http://127.0.0.1:57557", "http://127.0.0.1:57558"]
//   console.log(req.headers.origin); //拿到请求的来源地址
//   if (allowAdd.includes(req.headers.origin)) {
//     res.set("Access-Control-Allow-Origin", req.headers.origin)
//     res.set("Access-Control-Allow-Headers", "token")
//   }

//   next()
// })
// 
// cors跨域
// app.use((req,res,next)=>{
//   const allowAdd = ["http://127.0.0.1:5500", "http://127.0.0.1:57556", "http://127.0.0.1:57557", "http://127.0.0.1:57558"]
//   if (allowAdd.includes(req.headers.origin)) {
//       res.set("Access-Control-Allow-Origin", req.headers.origin)
//       res.set("Access-Control-Allow-Headers", "token")
//   }    
//   next()
// })
// app.use((req,res,next)=>{
//    const allowAdd = ['http://127.0.0.1:5510','http://127.0.0.1:5570','http://127.0.0.1:5508','http://127.0.0.1:5500']
//   if(allowAdd.includes(req.headers.origin)){
//     res.set('Access-Control-Allow-Origin',req.headers.origin)
//     res.set('Access-Control-Allow-headers','token')
//   }
//   next()
// })
app.use((req,res,next)=>{
  const allowAdd = ['http://127.0.0.1:5500','http://127.0.0.1:5003','http://127.0.0.1:5009','http://127.0.0.1:5006']
  if(allowAdd.includes(req.headers.origin)){
    res.set('Access-Control-Allow-Origin',req.headers.origin)
    res.set('Access-Control-Allow-Headers','token')
  }
  next()
})
app.get('/userInfo',(req,res)=>{
  res.send({
    code: 10000,
    msg: '查询成功',
    data: {
      name: '澜',
      age: 18,
      sex: '男'
    }
  })
})
// app.get("/userInfo", (req, res) => {
 

//   res.send({
//     code: 10000,
//     msg: "查询成功",
//     data: {
//       name: "李博",
//       age: 18,
//       sex: "男"
//     }
//   })
// })
app.get('/getUser',(req,res)=>{
  const {userId,cb} = req.query
  const data = {
    code: 10000,
    msg: '查询成功',
    data: {
      name: '展迟',
      age: 18,
      sex: '男'
    }
  }
  res.send(`${cb}(${JSON.stringify(data)})`)
})
// app.get('/getUser',(req,res)=>{
//   const{
//     userId,
//     cb
//   } = req.query
//   const data = {
//     code: 10000,
//     msg: '查询成功',
//     data: {
//       name: '东方镜',
//       age: 19,
//       sex: '女'
//     }
//   }
//   res.send(`${cb}(${JSON.stringify(data)})`)
// })
// //jsonp请求接口
// app.get("/getUser", (req, res) => {
//   console.log(req.query);
//   const {
//     userId,
//     cb
//   } = req.query;

//   const data = {
//     code: 10000,
//     msg: "查询成功",
//     data: {
//       name: "李博",
//       age: 18,
//       sex: "男"
//     }
//   }

//   res.send(`${cb}(${JSON.stringify(data)})`)
// })

//查询学生信息接口
app.get("/user", (req, res) => {
  console.log("收到请求");
  const {
    userId
  } = req.query;


  //如果查询失败
  if (userId !== "001") {
    return res.send({
      code: 10001,
      msg: "查询失败",
      data: null
    })
  }


  //查询成功 响应
  res.send({
    code: 10000,
    msg: "查询成功",
    data: {
      name: "李博",
      age: 18,
      sex: "男"
    }
  })

})

//添加接口
app.post("/adduser", (req,res) => {
  console.log("adduser");
  console.log(req.body, "req.body");
  console.log(req.query, "req.query");
  const {
    name,
    age,
    sex
  } = req.body;

  if (!name || !age || !sex) {
    return res.send({
      code: 10001,
      mes: "添加失败",
      data: null
    })
  }

  res.send({
    code: 10000,
    mes: "添加成功",
    data: null
  })
})

//修改接口
app.put("/updateuser", (req, res) => {
  const {
    userId,
    age,
    sex
  } = req.body;

  console.log(userId, age, sex, "更新");

  res.send({
    code: 10000,
    mes: "更新成功",
    data: null
  })
})

//删除接口
app.delete("/deleteuser", (req, res) => {
  const {
    userId
  } = req.query;
  console.log(userId, "删除");

  res.send({
    code: 10000,
    mes: "删除成功",
    data: null
  })
})


app.listen(3000, () => {
  console.log("服务器请求成功 请访问 http://127.0.0.1:3000");
})